package com.dckeji.shiplock.webapp.dao.impl;

import com.dckeji.shiplock.framework.util.DateTimeUtil;
import com.dckeji.shiplock.webapp.dao.BaseDao;
import com.dckeji.shiplock.webapp.dao.IEvalDao;
import com.dckeji.shiplock.webapp.pojo.Evaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/8
 * Time: 14:32
 */
@Repository
public class EvalDaoImpl extends BaseDao implements IEvalDao {


    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int commitShipLockList(final List<Evaluate> evaluates) {
        String sql = "INSERT INTO evaluate (shipNo,postId,optId,`desc`,commitTime) VALUES(?,?,?,?,?)";
        int[] ints = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Evaluate evaluate = evaluates.get(i);
                preparedStatement.setString(1, evaluate.getShipNo());
                preparedStatement.setInt(2, evaluate.getPostId());
                preparedStatement.setInt(3, evaluate.getOptId());
                preparedStatement.setString(4, evaluate.getDesc());
                preparedStatement.setString(5, DateTimeUtil.getSystemTime4Day());
            }

            @Override
            public int getBatchSize() {
                return evaluates.size();
            }
        });
        return validaBatch(ints);
    }

    @Override
    public List<Evaluate> queryTime(Evaluate evaluate) {
        String sql = "select DISTINCT(commitTime) from evaluate where shipNo=?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Evaluate>(Evaluate.class), new Object[]{evaluate.getShipNo()});
    }

    @Override
    public List<Evaluate> queryEvaluateInfos(Evaluate evaluate) {

        String sql = "select * from evaluate where commitTime=?  and shipNo=?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Evaluate>(Evaluate.class), new Object[]{evaluate.getCommitTime(), evaluate.getShipNo()});
    }
}
